﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;

namespace SnmpForm.APConfig
{
    public class frmApConfig : Form
    {
        public frmApConfig()
        {
            InitializeComponent();
        }
        public frmApConfig(BusinessWrapper.Ap ap)
        {
            m_ap = ap;
            InitializeComponent();
        }
        protected BusinessWrapper.Ap m_ap = null;
        public byte[] IpByteByStr(string strIp)
        {
            try
            {
                System.Net.IPAddress a = System.Net.IPAddress.Parse(strIp);
                return a.GetAddressBytes();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
                return null;
            }
        }
        public string IpStrByByte(byte[] ip)
        {
            System.Net.IPAddress a = new System.Net.IPAddress(ip);
            return a.ToString();
        }

        public delegate void CloseForm();
        public CloseForm closeForm;
        public void CloseFormMethod()
        {
            Close();
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // ApConfigForm
            // 
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Name = "ApConfigForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.ResumeLayout(false);

            //自定义关闭窗口委托
            closeForm = new CloseForm(CloseFormMethod);
        }

        public virtual bool Update(bool bView=true)
        {
            return true;
        }

        public virtual bool SaveToDb()
        {
            Update(false);
            bool bSuccess =
                BusinessWrapper.User.Instance.ModifyWirelessInfo(m_ap);
            if (bSuccess)
            {
                MessageBox.Show("保存成功");
                Close();
            }
            else
            {
                MessageBox.Show("保存失败");
            }
            return bSuccess;
        }

        public virtual bool SyncToApCommit()
        {
            return true;
        }

        public bool SyncToAp()
        {
            bool bSync = false;
            try
            {
                bSync = SyncToApCommit();
            }
            catch (Lextm.SharpSnmpLib.Messaging.TimeoutException ex)
            {
                Console.WriteLine(ex.Message.ToString());
                MessageBox.Show("同步超时");
            }
            catch (SocketException ex)
            {
                Console.WriteLine(ex.Message.ToString());
                MessageBox.Show("连接异常");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
                MessageBox.Show(ex.Message, "异常");
            }
            return bSync;
        }
    }
}
